/***
 * Copyright (c) 2010 readyState Software Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.softserveinc.moviedbapp.map;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.OverlayItem;
import com.softserveinc.moviedbapp.R;

public class BalloonOverlayView extends FrameLayout {

	private LinearLayout layout;
	private TextView title;
	private TextView snippet;

	public BalloonOverlayView( final Context context, final int balloonBottomOffset ) {

		super( context );

		setPadding( 10, 0, 10, balloonBottomOffset );
		layout = new LinearLayout( context );
		layout.setVisibility( VISIBLE );

		LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View v = inflater.inflate( R.layout.balloon_overlay, layout );
		title = (TextView) v.findViewById( R.id.balloon_item_title );
		snippet = (TextView) v.findViewById( R.id.balloon_item_snippet );

		ImageView close = (ImageView) v.findViewById( R.id.close_img_button );
		close.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick( final View v ) {
				layout.setVisibility( GONE );
			}
		} );

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT );
		params.gravity = Gravity.NO_GRAVITY;

		addView( layout, params );

	}

	public void setData( final OverlayItem item ) {

		layout.setVisibility( VISIBLE );
		if ( item.getTitle() != null ) {
			title.setVisibility( VISIBLE );
			title.setText( item.getTitle() );
		} else {
			title.setVisibility( GONE );
		}
		if ( item.getSnippet() != null ) {
			snippet.setVisibility( VISIBLE );
			snippet.setText( item.getSnippet() );
		} else {
			snippet.setVisibility( GONE );
		}

	}

}
